<?php
namespace BP\Sys;
require_once("BP/En/FieldTypeS.php");
require_once("BP/En/UIContralType.php");
require_once("BP/Tools/Json.php");
require_once("net/sf/json/JSONArray.php");
require_once("net/sf/json/JSONObject.php");
class CCFormParse {
	public static function SaveFrmEle ($fk_mapdata, $eleType, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String eleType, String ctrlID, float x, float y, float h, float w]
	{
		$en = FrmEle::constructor__();
		$en->setEleType($eleType);
		$en->setFK_MapData($fk_mapdata);
		$en->setEleID($ctrlID);
		$i = $en->Retrieve(FrmEleAttr::$FK_MapData, $fk_mapdata, FrmEleAttr::$EleID, $ctrlID);
		$en->setX($x);
		$en->setY($y);
		$en->setW($w);
		$en->setH($h);
		if (($i == 0))
		{
			$en->Insert();
		}
		else
		{
			$en->Update();
		}
	}
	public static function SaveMapFrame ($fk_mapdata, $eleType, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String eleType, String ctrlID, float x, float y, float h, float w]
	{
		$en = MapFrame::constructor__();
		$en->setMyPK($ctrlID);
		$i = $en->RetrieveFromDBSources();
		$en->setEleType($eleType);
		$en->setFK_MapData($fk_mapdata);
		$en->setFrmID($ctrlID);
		$en->setX($x);
		$en->setY($y);
		$en->setW($w);
		$en->setH($h);
		if (($i == 0))
		{
			$en->Insert();
		}
		else
		{
			$en->Update();
		}
	}
	public static function SaveFrmRadioButton ($fk_mapdata, $ctrlID, $x, $y) // [String fk_mapdata, String ctrlID, float x, float y]
	{
		$en = FrmRB::constructor__();
		$en->setMyPK((($fk_mapdata . "_") . $ctrlID));
		$i = $en->RetrieveFromDBSources();
		if (($i == 0))
		{
			return NULL;
		}
		$en->setFK_MapData($fk_mapdata);
		$en->setX($x);
		$en->setY($y);
		$en->Update();
		return $en->getKeyOfEn();
	}
	public static function SaveAthImg ($fk_mapdata, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String ctrlID, float x, float y, float h, float w]
	{
		$en = FrmImgAth::constructor__();
		$en->setMyPK((($fk_mapdata . "_") . $ctrlID));
		$en->setFK_MapData($fk_mapdata);
		$en->setCtrlID($ctrlID);
		$en->RetrieveFromDBSources();
		$en->setX($x);
		$en->setY($y);
		$en->setW($w);
		$en->setH($h);
		$en->Update();
	}
	public static function SaveAthMulti ($fk_mapdata, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String ctrlID, float x, float y, float h, float w]
	{
		$en = FrmAttachment::constructor__();
		$en->setMyPK((($fk_mapdata . "_") . $ctrlID));
		$en->setFK_MapData($fk_mapdata);
		$en->setNoOfObj($ctrlID);
		$en->RetrieveFromDBSources();
		$en->setX($x);
		$en->setY($y);
		$en->setW($w);
		$en->setH($h);
		$en->Update();
	}
	public static function SaveDtl ($fk_mapdata, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String ctrlID, float x, float y, float h, float w]
	{
		$dtl = MapDtl::constructor__();
		$dtl->setNo($ctrlID);
		$dtl->RetrieveFromDBSources();
		$dtl->setFK_MapData($fk_mapdata);
		$dtl->setX($x);
		$dtl->setY($y);
		$dtl->setW($w);
		$dtl->setH($h);
		$dtl->Update();
	}
	public static function SaveiFrame ($fk_mapdata, $ctrlID, $x, $y, $h, $w) // [String fk_mapdata, String ctrlID, float x, float y, float h, float w]
	{
		$en = FrmEle::constructor__();
		$en->setFK_MapData($fk_mapdata);
		$en->setEleID($ctrlID);
		$en->setMyPK((($en->getFK_MapData() . "_") . $en->getEleID()));
		if (($en->RetrieveFromDBSources() == 0))
		{
			$en->Insert();
		}
		$en->setX($x);
		$en->setY($y);
		$en->setW($w);
		$en->setH($h);
		$en->Update();
	}
	public static function SaveMapAttr ($fk_mapdata, $fieldID, $shape, $control, $properties, $pks) // [String fk_mapdata, String fieldID, String shape, JSONObject control, JSONArray properties, String pks]
	{
		$attr = MapAttr::constructor__();
		$attr->setFK_MapData($fk_mapdata);
		$attr->setKeyOfEn($fieldID);
		$attr->setMyPK((($fk_mapdata . "_") . $fieldID));
		$attr->RetrieveFromDBSources();
		switch ($shape) {
			case "TextBoxStr":
			case "TextBoxSFTable":
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "TextBoxInt":
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setMyDataType($DataType->AppInt);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "TextBoxBoolean":
				$attr->setMyDataType($DataType->AppBoolean);
				$attr->setUIContralType($UIContralType->CheckBok);
				$attr->setLGType($FieldTypeS->Normal);
				break;
			case "TextBoxFloat":
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "TextBoxMoney":
				$attr->setMyDataType($DataType->AppMoney);
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "TextBoxDate":
				$attr->setMyDataType($DataType->AppDate);
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "TextBoxDateTime":
				$attr->setMyDataType($DataType->AppDateTime);
				$attr->setLGType($FieldTypeS->Normal);
				$attr->setUIContralType($UIContralType->TB);
				break;
			case "DropDownListEnum":
				$attr->setMyDataType(DataType->AppInt);
				$attr->setLGType($FieldTypeS->Enum);
				$attr->setUIContralType($UIContralType->DDL);
				break;
			case "DropDownListTable":
				$attr->setMyDataType(DataType->AppString);
				if (($pks->contains((("@" . $attr->getKeyOfEn()) . "@")) ==  FALSE ))
				{
					$attr->setLGType($FieldTypeS->FK);
				}
				$attr->setUIContralType($UIContralType->DDL);
				$attr->setMaxLen(100);
				$attr->setMinLen(0);
				break;
			default:
				break;
		}
		$style = $control->getJSONObject("style");
		$vector = $style->getJSONArray("gradientBounds");
		$attr->setX($Float->parseFloat($vector->get(0)));
		$attr->setY($Float->parseFloat($vector->get(1)));
		for ($iProperty = 0; ($iProperty < $properties->size()); ++$iProperty) 
		{
			$property = $properties->getJSONObject($iProperty);
			if ((((($property == NULL) || !$property->containsKey("property")) || ($property->get("property") == NULL)) || $property->get("property")->equals("group")))
			{
				continue;
			}
			$val = NULL;
			if (($property->get("PropertyValue") != NULL))
			{
				$val = $property->get("PropertyValue");
			}
			$propertyName = $property->get("property");
			switch ($propertyName) {
				case "Name":
					if ($attr->getName()->equals(""))
					{
						$attr->setName($val);
					}
					break;
				case "MinLen":
				case "MaxLen":
				case "DefVal":
					$attr->SetValByKey($propertyName, $val);
					break;
				case "UIIsEnable":
				case "UIVisible":
					$attr->SetValByKey($propertyName, $val);
					break;
				case "FieldText":
					if ($attr->getName()->equals(""))
					{
						$attr->setName($val);
					}
					break;
				case "UIIsInput":
					if ($val->equals("true"))
					{
						$attr->setUIIsInput( TRUE );
					}
					else
					{
						$attr->setUIIsInput( FALSE );
					}
					break;
				case "UIBindKey":
					$attr->setUIBindKey($val);
					break;
				default:
					break;
			}
		}
		$minX = new BigDecimal($vector->get(0));
		$minY = new BigDecimal($vector->get(1));
		$maxX = new BigDecimal($vector->get(2));
		$maxY = new BigDecimal($vector->get(3));
		$imgWidth = new BigDecimal(($maxX->intValue() - $minX->intValue()));
		$imgHeight = new BigDecimal(($maxY->intValue() - $minY->intValue()));
		$attr->setUIWidth($Float->parseFloat($String->format("%.2f", $imgWidth)));
		$attr->setUIHeight($Float->parseFloat($String->format("%.2f", $imgHeight)));
		if (($pks->contains((("@" . $attr->getKeyOfEn()) . "@")) ==  TRUE ))
		{
			$attr->Update();
		}
		else
		{
			$attr->Insert();
		}
	}
	public static function SaveLine ($fk_mapdata, $form_Lines) // [String fk_mapdata, JSONArray form_Lines]
	{
		$linePKs = "@";
		$lines = FrmLines::constructor__();
		$lines->Retrieve(FrmLabAttr::$FK_MapData, $fk_mapdata);
		foreach ($lines->ToJavaList() as $item) 		{
			$linePKs += ($item->getMyPK() . "@");
		}
		if ((($form_Lines->isArray() ==  TRUE ) && ($form_Lines->size() > 0)))
		{
			for ($idx = 0$jLine = $form_Lines->size(); ($idx < $jLine); ++$idx) 
			{
				$line = $form_Lines->getJSONObject($idx);
				if ($line->isNullObject())
				{
					continue;
				}
				$lineEn = FrmLine::constructor__();
				$lineEn->setMyPK($line->get("CCForm_MyPK"));
				$lineEn->setFK_MapData($fk_mapdata);
				$turningPoints = $line->getJSONArray("turningPoints");
				$lineEn->setX1($Float->parseFloat($turningPoints->getJSONObject(0)->getString("x")));
				$lineEn->setX2($Float->parseFloat($turningPoints->getJSONObject(1)->getString("x")));
				$lineEn->setY1($Float->parseFloat($turningPoints->getJSONObject(0)->getString("y")));
				$lineEn->setY2($Float->parseFloat($turningPoints->getJSONObject(1)->getString("y")));
				$properties = $line->getJSONArray("properties");
				$borderWidth = $Json->GetObjectFromArrary_ByKeyValue($properties, "type", "LineWidth");
				$borderColor = $Json->GetObjectFromArrary_ByKeyValue($properties, "type", "Color");
				$strborderWidth = "2";
				if (((($borderWidth != NULL) && ($borderWidth->get("PropertyValue") != NULL)) && !$DataType->IsNullOrEmpty($borderWidth->get("PropertyValue"))))
				{
					$strborderWidth = $borderWidth->get("PropertyValue");
				}
				$strBorderColor = "Black";
				if (((($borderColor != NULL) && ($borderColor->get("PropertyValue") != NULL)) && !$DataType->IsNullOrEmpty($borderColor->get("PropertyValue"))))
				{
					$strBorderColor = $borderColor->get("PropertyValue");
				}
				$lineEn->setBorderWidth($Float->parseFloat($strborderWidth));
				$lineEn->setBorderColor($strBorderColor);
				if (($linePKs->contains((("@" . $lineEn->getMyPK()) . "@")) ==  TRUE ))
				{
					$linePKs = $linePKs->replace(($lineEn->getMyPK() . "@"), "");
					$lineEn->DirectUpdate();
				}
				else
				{
					$lineEn->DirectInsert();
				}
			}
		}
		$strs = $linePKs->split("[@]", -1);
		$sqls = "";
		foreach ($strs as $str) 		{
			if ($DataType->IsNullOrEmpty($str))
			{
				continue;
			}
			$sqls += (("@DELETE FROM Sys_FrmLine WHERE MyPK='" . $str) . "'");
		}
		if (!$sqls->equals(""))
		{
			DBAccess->RunSQLs($sqls);
		}
	}
	public static function SaveLabel ($fk_mapdata, $control, $properties, $pks, $ctrlID) // [String fk_mapdata, JSONObject control, JSONArray properties, String pks, String ctrlID]
	{
		$lab = FrmLab::constructor__();
		$lab->setMyPK($ctrlID);
		$lab->setFK_MapData($fk_mapdata);
		$style = $control->getJSONObject("style");
		$vector = $style->getJSONArray("gradientBounds");
		$lab->setX($Float->parseFloat($vector->get(0)));
		$lab->setY($Float->parseFloat($vector->get(1)));
		$fontStyle = new StringBuilder();
		for ($iProperty = 0; ($iProperty < $properties->size()); ++$iProperty) 
		{
			$property = $properties->getJSONObject($iProperty);
			if (((($property == NULL) || !$property->containsKey("type")) || ($property->optString("type") == NULL)))
			{
				continue;
			}
			$type = $property->get("type")->trim();
			$val = NULL;
			if (($property->get("PropertyValue") != NULL))
			{
				$val = $property->get("PropertyValue");
			}
			switch ($type) {
				case "SingleText":
					$lab->setText(( (($val . NULL)) ? "" : $val->replace(" ", "&nbsp;")->replace("\n", "@") ));
					break;
				case "Color":
					$lab->setFontColor(( (($val . NULL)) ? "#000000" : $val ));
					$fontStyle->append($String->format("color:%1$s;", $lab->getFontColor()));
					break;
				case "TextFontFamily":
					$lab->setFontName(( (($val . NULL)) ? "Portable User Interface" : $val ));
					if (($val . NULL))
					{
						$fontStyle->append($String->format("font-family:%1$s;", $property->optString("PropertyValue")));
					}
					break;
				case "TextFontSize":
					$lab->setFontSize(( (($val . NULL)) ? 14 : $Integer->parseInt($val) ));
					$fontStyle->append($String->format("font-size:%1$s;", $lab->getFontSize()));
					break;
				case "FontWeight":
					if ((($val . NULL) . $val->equals("normal")))
					{
						$lab->setIsBold( FALSE );
						$fontStyle->append($String->format("font-weight:normal;"));
					}
					else
					{
						$lab->setIsBold( TRUE );
						$fontStyle->append($String->format("font-weight:%1$s;", $val));
					}
					break;
				default:
					break;
			}
		}
		if ((($lab->getText() . NULL) . $lab->getText()->equals("")))
		{
			$primitives = $control->getJSONArray("primitives")->get(0);
			$lab->setText($primitives->get("str")->trim());
			$lab->setFontName($primitives->get("font")->trim());
			$lab->setFontSize($Integer->parseInt($primitives->get("size")->trim()));
		}
		$lab->setFontStyle($fontStyle);
		if (($pks->contains(($lab->getMyPK() . "@")) ==  TRUE ))
		{
			$lab->DirectUpdate();
		}
		else
		{
			$lab->DirectInsert();
		}
	}
	public static function SaveButton ($fk_mapdata, $control, $properties, $pks, $ctrlID) // [String fk_mapdata, JSONObject control, JSONArray properties, String pks, String ctrlID]
	{
		$btn = FrmBtn::constructor__String($ctrlID);
		$btn->setMyPK($ctrlID);
		$btn->setFK_MapData($fk_mapdata);
		$style = $control->getJSONObject("style");
		$vector = $style->getJSONArray("gradientBounds");
		$btn->setX($Float->parseFloat($vector->get(0)));
		$btn->setY($Float->parseFloat($vector->get(1)));
		$btn->setIsEnable( TRUE );
		if (($pks->contains((("@" . $btn->getMyPK()) . "@")) ==  TRUE ))
		{
			$btn->DirectUpdate();
		}
		else
		{
			$btn->DirectInsert();
		}
	}
	public static function SaveHyperLink ($fk_mapdata, $control, $properties, $pks, $ctrlID) // [String fk_mapdata, JSONObject control, JSONArray properties, String pks, String ctrlID]
	{
		$link = FrmLink::constructor__String($ctrlID);
		$link->setMyPK($ctrlID);
		$link->setFK_MapData($fk_mapdata);
		$vector = $control->getJSONObject("style")->getJSONArray("gradientBounds");
		$link->setX($Float->parseFloat($vector->get(0)));
		$link->setY($Float->parseFloat($vector->get(1)));
		$fontStyle = new StringBuilder();
		for ($iProperty = 0; ($iProperty < $properties->size()); ++$iProperty) 
		{
			$property = $properties->getJSONObject($iProperty);
			if (((($property == NULL) || !$property->containsKey("property")) || ($property->optString("property") == NULL)))
			{
				continue;
			}
			$propertyLink = $property->get("property");
			$valLink = $property->optString("PropertyValue");
			switch ($propertyLink) {
				case "primitives.0.style.fillStyle":
					$link->setFontColor(( (($valLink . NULL)) ? "#FF000000" : $valLink ));
					$fontStyle->append($String->format("color:%1$s;", $link->getFontColor()));
					break;
				case "FontName":
					$link->setFontName(( (($valLink . NULL)) ? "Portable User Interface" : $valLink ));
					if (($valLink . NULL))
					{
						$fontStyle->append($String->format("font-family:%1$s;", $Json->ToJson($valLink)));
					}
					continue;
				case "FontSize":
					$link->setFontSize(( (($valLink . NULL)) ? 14 : $Integer->parseInt($valLink) ));
					$fontStyle->append($String->format("font-size:%1$s;", $link->getFontSize()));
					continue;
				case "primitives.0.fontWeight":
					if ((($valLink . NULL) . $valLink->equals("normal")))
					{
						$link->setIsBold( FALSE );
						$fontStyle->append($String->format("font-weight:normal;"));
					}
					else
					{
						$link->setIsBold( TRUE );
						$fontStyle->append($String->format("font-weight:%1$s;", $valLink));
					}
					continue;
				case "FontColor":
					$link->setFontColor(( (($valLink . NULL)) ? "" : $valLink ));
					continue;
				default:
					break;
			}
		}
		$link->setFontStyle($fontStyle);
		if ((($link->getText() . NULL) . $link->getText()->equals("")))
		{
			$primitives = $control->getJSONArray("primitives")->get(0);
			$link->setText($primitives->get("str")->trim());
			$link->setFontName($primitives->get("font")->trim());
			$link->setFontSize($Integer->parseInt($primitives->get("size")->trim()));
		}
		if (($pks->contains((("@" . $link->getMyPK()) . "@")) ==  TRUE ))
		{
			$link->DirectUpdate();
		}
		else
		{
			$link->DirectInsert();
		}
	}
	public static function SaveImage ($fk_mapdata, $control, $properties, $pks, $ctrlID) // [String fk_mapdata, JSONObject control, JSONArray properties, String pks, String ctrlID]
	{
		$img = FrmImg::constructor__();
		$img->setMyPK($ctrlID);
		$count = $img->RetrieveFromDBSources();
		$img->setFK_MapData($fk_mapdata);
		$img->setIsEdit(1);
		$img->setHisImgAppType(ImgAppType::$Img);
		$vector = $control->getJSONObject("style")->getJSONArray("gradientBounds");
		$img->setX($Float->parseFloat($vector->get(0)));
		$img->setY($Float->parseFloat($vector->get(1)));
		$minX = new BigDecimal($vector->get(0));
		$minY = new BigDecimal($vector->get(1));
		$maxX = new BigDecimal($vector->get(2));
		$maxY = new BigDecimal($vector->get(3));
		$imgWidth = new BigDecimal(($maxX->intValue() - $minX->intValue()));
		$imgHeight = new BigDecimal(($maxY->intValue() - $minY->intValue()));
		$img->setW($Float->parseFloat($String->format("%.2f", $imgWidth)));
		$img->setH($Float->parseFloat($String->format("%.2f", $imgHeight)));
		$fontStyle = new StringBuilder();
		for ($iProperty = 0; ($iProperty < $properties->size()); ++$iProperty) 
		{
			$property = $properties->getJSONObject($iProperty);
			if (((($property == NULL) || !$property->containsKey("property")) || ($property->get("property") == NULL)))
			{
				continue;
			}
			if ($property->get("property")->equals("LinkURL"))
			{
				$img->setLinkURL(( (($property->get("PropertyValue") == NULL)) ? "" : $property->get("PropertyValue") ));
			}
			else
				if ($property->get("property")->equals("WinOpenModel"))
				{
					$img->setLinkTarget(( (($property->get("PropertyValue") == NULL)) ? "_blank" : $property->get("PropertyValue") ));
				}
				else
					if ($property->get("property")->equals("ImgAppType"))
					{
						$img->setImgSrcType(( (($property->get("PropertyValue") == NULL)) ? 0 : $Integer->parseInt($property->get("PropertyValue")) ));
					}
					else
						if ($property->get("property")->equals("ImgPath"))
						{
							$img->setImgURL(( (($property->get("PropertyValue") == NULL)) ? "" : $property->get("PropertyValue") ));
						}
		}
		$primitives = $control->getJSONArray("primitives");
		$primitive = NULL;
		for ($i = 0; ($i < $primitives->size()); ++$i) 
		{
			$primitive = $primitives->getJSONObject($i);
			if ((NULL == $primitive->optString("oType")))
			{
				continue;
			}
			if (($primitive->optString("oType")->equals("ImageFrame") && ($count == 0)))
			{
				$img->setImgPath(( (($primitive == NULL)) ? "" : $primitive->optString("url") ));
			}
		}
		if (($pks->contains(($img->getMyPK() . "@")) ==  TRUE ))
		{
			$img->DirectUpdate();
		}
		else
		{
			$img->DirectInsert();
		}
	}
}
?>
